@using OfficeOpenXml.FormulaParsing.Excel.Functions.Math
@model AffiliateSearchModel

@{
    //page title
    ViewBag.PageTitle = T("Admin.Affiliates").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Affiliates");
}

@{
    const string hideSearchBlockAttributeName = "AffiliatePage.HideSearchBlock";
    var hideSearchBlock = genericAttributeService.GetAttribute<bool>(workContext.CurrentCustomer, hideSearchBlockAttributeName);
}

<div class="content-header clearfix">
    <h1 class="pull-left">
        @T("Admin.Affiliates")
    </h1>
    <div class="pull-right">
        <a asp-action="Create" class="btn bg-blue">
            <i class="fa fa-plus-square"></i>
            @T("Admin.Common.AddNew")
        </a>
        @await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.AffiliateListButtons })
    </div>
</div>

<script>
    $(document).ready(function() {
        $("#@Html.IdFor(model => model.LoadOnlyWithOrders)").click(toggleLoadOnlyWithOrders);
        toggleLoadOnlyWithOrders();
    });

    function toggleLoadOnlyWithOrders() {
        if ($('#@Html.IdFor(model => model.LoadOnlyWithOrders)').is(':checked')) {
            $('#pnlOrdersCreatedFromUtc').show();
            $('#pnlOrdersCreatedToUtc').show();
        } else {
            $('#pnlOrdersCreatedFromUtc').hide();
            $('#pnlOrdersCreatedToUtc').hide();
        }
    }
</script>

<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default panel-search">
                <div class="panel-body">
                    <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                        <div class="search-text">@T("Admin.Common.Search")</div>
                        <div class="icon-search"><i class="fa fa-search" aria-hidden="true"></i></div>
                        <div class="icon-collapse"><i class="fa fa-angle-down" aria-hidden="true"></i></div>
                    </div>

                    <div class="search-body @(hideSearchBlock ? "closed" : "")">
                        <p>
                            @T("Admin.Affiliates.Description")
                        </p>
                        <div class="row">
                            <div class="col-md-5">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchFirstName" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="SearchFirstName" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchLastName" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="SearchLastName" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-7">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchFriendlyUrlName" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="SearchFriendlyUrlName" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="LoadOnlyWithOrders" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="LoadOnlyWithOrders" />
                                    </div>
                                </div>
                                <nop-nested-setting asp-for="LoadOnlyWithOrders">
                                    <div class="form-group" id="pnlOrdersCreatedFromUtc">
                                        <div class="col-md-4">
                                            <nop-label asp-for="OrdersCreatedFromUtc" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="OrdersCreatedFromUtc" />
                                        </div>
                                    </div>
                                    <div class="form-group" id="pnlOrdersCreatedToUtc">
                                        <div class="col-md-4">
                                            <nop-label asp-for="OrdersCreatedToUtc" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="OrdersCreatedToUtc" />
                                        </div>
                                    </div>
                                </nop-nested-setting>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-7 col-md-offset-5">
                                <button type="button" id="search-affiliates" class="btn btn-primary btn-search">
                                    <i class="fa fa-search"></i>
                                    @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-body">
                    @await Html.PartialAsync("Table", new DataTablesModel
                    {
                        Name = "affiliates-grid",
                        UrlRead = new DataUrl("List", "Affiliate", null),
                        SearchButtonId = "search-affiliates",
                        Length = Model.PageSize,
                        LengthMenu = Model.AvailablePageSizes,
                        Filters = new List<FilterParameter>
                        {
                            new FilterParameter(nameof(Model.SearchFirstName)),
                            new FilterParameter(nameof(Model.SearchLastName)),
                            new FilterParameter(nameof(Model.SearchFriendlyUrlName)),
                            new FilterParameter(nameof(Model.LoadOnlyWithOrders), typeof(bool)),
                            new FilterParameter(nameof(Model.OrdersCreatedFromUtc)),
                            new FilterParameter(nameof(Model.OrdersCreatedToUtc))
                        },
                        ColumnCollection = new List<ColumnProperty>
                        {
                            new ColumnProperty($"{nameof(AffiliateModel.Address)}.{nameof(AddressModel.FirstName)}")
                            {
                                Title = T("Admin.Address.Fields.FirstName").Text,
                                Width = "200"
                            },
                            new ColumnProperty($"{nameof(AffiliateModel.Address)}.{nameof(AddressModel.LastName)}")
                            {
                                Title = T("Admin.Address.Fields.LastName").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(AffiliateModel.Active))
                            {
                                Title = T("Admin.Affiliates.Fields.Active").Text,
                                Width = "100",
                                ClassName = NopColumnClassDefaults.CenterAll,
                                Render = new RenderBoolean()
                            },
                            new ColumnProperty(nameof(AffiliateModel.Id))
                            {
                                Title = T("Admin.Common.Edit").Text,
                                Width = "100",
                                ClassName = NopColumnClassDefaults.Button,
                                Render = new RenderButtonEdit(new DataUrl("Edit"))
                            }
                        }
                    })
                </div>
            </div>
        </div>
    </div>
</div>